package com.srpingde.hhh;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class AboutActivity extends Activity {
	ImageView erWeiMa,back;
	TextView showVersion,about_banquan;
	HHHIml weiQuIml;
	Button about_lisnxi;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about);
		init();
		
		//
		
		///211
	}
	//点击事件
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.about_back:
			finish();
			break;
		case R.id.about_lisnxi:
			if(!about_lisnxi.getText().equals("")){
				MakeThePhoneCall(about_lisnxi.getText().toString());
			}
			break;
		case R.id.about_banquan:
			
			break;
		}
	}
	//初始化
	private void init(){
		erWeiMa = (ImageView) findViewById(R.id.about_erweima);
		back = (ImageView) findViewById(R.id.about_back);
		showVersion = (TextView) findViewById(R.id.about_showversion);
		about_lisnxi = (Button) findViewById(R.id.about_lisnxi);
		about_banquan = (TextView) findViewById(R.id.about_banquan);
		showVersion.setText("软件版本：\rV"+HhhApp.getInstance().getVersion(AboutActivity.this));
		weiQuIml = new HHHIml();
		weiQuIml.getAboutHttp(AboutActivity.this, new AsyncHttpResponseHandler(){

			@Override
			public void onSuccess(String content) {
				// TODO Auto-generated method stub
				super.onSuccess(content);
				try {
					JSONObject jsonObject = new JSONObject(content);
					JSONArray array = jsonObject.getJSONArray("rows");
					for(int i = 0;i<array.length();i++){
						JSONObject object = array.getJSONObject(i);
						about_lisnxi.setText(about_lisnxi.getText()+object.getString("tel"));
						about_banquan.setText(about_banquan.getText()+object.getString("copyright"));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		layout();
		try {
			erWeiMa.setImageBitmap(CreateTwoDCode(HHHInterface.httpHeader + "apk/2014061015144762.apk"));
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void getdata(){
		
	}
	/**
	 * 将指定的内容生成成二维码
	 * 
	 * @param content
	 *            将要生成二维码的内容
	 * @return 返回生成好的二维码事件
	 * @throws WriterException
	 *             WriterException异常
	 */
	public Bitmap CreateTwoDCode(String content) throws WriterException {
		// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
		BitMatrix matrix = new MultiFormatWriter().encode(content,
				BarcodeFormat.QR_CODE, 300, 300);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		// 二维矩阵转为一维像素数组,也就是一直横着排了
		int[] pixels = new int[width * height];
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = 0xff000000;
				}
			}
		}

		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		// 通过像素数组生成bitmap,具体参考api
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}
	
	private void layout() {
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.about_title);
		RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams) top_title_tv.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		//返回按钮
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
	}
	//打电话
	public void MakeThePhoneCall(final String content){
		new AlertDialog.Builder(AboutActivity.this)
		.setTitle("提示")
		.setMessage("确定拨打"+content+"？")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Intent in = new Intent();
				in.setAction(Intent.ACTION_CALL);
				Uri uri = Uri.parse("tel:"+content);
				in.setData(uri);
				in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
				startActivity(in);
				}
			}).setNegativeButton("取消", null).create().show();
		}
}
